package com.cddata.util;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import com.cddata.HelperBean;
import com.cddata.entity.ContentType;
import com.cddata.entity.Contents;

public class DBOperation {
	public static Session createAndInsertContent( Session session, String aFileName, String aPath, String aSize  ) throws Exception {
            try {
                
            
		int fileIndex = aPath.lastIndexOf("\\");
		int endIndex = aPath.length();
		String filePath = aPath.substring(0,fileIndex);
		String dataFileName = aPath.substring(filePath.length()+1,endIndex);
		int  extIndex = dataFileName.lastIndexOf(".");
		String exten ="NONE";
		// if the extIndex is -1 then there is no extension for this file
		if( extIndex != -1 ) {
			exten = dataFileName.substring( extIndex+1, dataFileName.length() );
			dataFileName = dataFileName.substring(0, extIndex);
		}
		if(  "".equalsIgnoreCase(dataFileName) ) {
			dataFileName = exten;
		}
		//	find the exact file name from the aPath without the extension 

		// this is done in order to get rid of the file extensions with no file name like ex : "\.lock"  
		// and if the extension index is less than the file index ex: "\.history\27\d042ca1711ae001b1635beee85f036e9"

		int typeId = 0;
		if( !"-".equalsIgnoreCase( exten ) ) {
			typeId = getTypeId( exten.toUpperCase() );		
		}
		Contents content = new Contents();
		content.setContentHolder(aFileName.substring(0,aFileName.indexOf(".")));
		content.setTypeId(typeId);
		content.setContentName(dataFileName);
		content.setContentPath(filePath);
		content.setContentSize(new Long(aSize));
		content.setContentId(DBUtil.getNewRowId("Contents", "contentId"));
		insertData( session, content );
		} catch (Exception e) {
            }
		return session;
	}	

	public static int getTypeId( String aExtension ) {
		int typeId = 0; 
		if( !helperBean.contentTypes.containsValue(aExtension) ) {
			typeId =insertType(aExtension);
		} else {
			typeId = new Integer(helperBean.getTypeId(aExtension) );
		}
		return typeId;
	}
	
	public static int insertType( String aExtension ) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		ContentType ct = new ContentType();
		ct.setTypeId(DBUtil.getNewRowId("ContentType", "typeId"));
		ct.setTypeName(aExtension);
		session.save(ct);
		tx.commit();
		helperBean.setContentTypes(ct);
		session.close();
		return ( ct.getTypeId() ); 
	}
	/**
	 * 
	 * @param aCon
	 * @param aContent
	 */
	public static void insertData( Session aSession, Contents aContent ) throws Exception{
		aSession.save(aContent);
	}
	
	@SuppressWarnings("unchecked")
	public static void populateContentTypes() {
		List<ContentType> list = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(ContentType.class).addOrder(Order.asc("typeName"));
		list = criteria.list();
		session.close();
		for( Iterator<ContentType> it = list.iterator(); it.hasNext();) {
			ContentType ct = it.next();
			helperBean.setContentTypes(ct);
		}
	}
	
	public static HelperBean getHelperBean() {
		return helperBean;
	}

	public static void setHelperBean(HelperBean helperBean) {
		DBOperation.helperBean = helperBean;
	}
	
	private static HelperBean helperBean;
}
